package com.lqq.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lqq.dto.UserDetailDto;
import com.lqq.entity.User;
import com.lqq.mapper.UserMapper;
import com.lqq.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author qingqing.liang
 * @create 2018-01-30
 **/
@Service
@Slf4j
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public User getById(Long id) {
        log.info("userSerivce invoked");
        return userMapper.getById(id);

    }

    @Override
    public User getUserDetailById(Long id) {
        return userMapper.getUserDetailById(id);
    }

    @Override
    public UserDetailDto getUserDetailById2(Long id) {
        return userMapper.getUserDetailById2(id);
    }

    @Override
    public PageInfo<User> getUsers(int pageSize, int currentPage) {
        PageHelper.startPage(currentPage,pageSize);
        List<User> users = userMapper.selectUsers();
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public void updateBatch(Long[] ids, int status) {
        Map<String,Object> param = new HashMap<>();
        param.put("ids",ids);
        param.put("status",status);
        userMapper.updateBatch(param);
    }

    @Override
    public PageInfo<User> findByStatusAndNameLike(Integer status, String name) {

        Map<String,Object> param = new HashMap<>();
        param.put("name",name);
        param.put("status",status);
        PageHelper.startPage(1,3);
        List<User> users = userMapper.findByStatusAndNameLike(param);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public PageInfo<User> chooseWhenOtherwise(Integer status, String name) {
        Map<String,Object> param = new HashMap<>();
        param.put("name",name);
        param.put("status",status);
        PageHelper.startPage(1,3);
        List<User> users = userMapper.chooseWhenOtherwise(param);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }


}
